package dao;
import java.util.List;

import excepciones.DaoException;
import modelo.Persona;

/**
 * Persona DAO: Maintains Persistence between model and database
 * Based on Hibernate JPA
 */
public class PersonaDao extends AbstractDao
{

   /**
    * Class Constructor [Empty]
    */
   public PersonaDao()
   {
      super();
   }
   
   /**
    * Class Constructor
    * @param model   The Persona model to manage persistence for
    */
   public PersonaDao(Persona PersonaModel)
   {
      super(PersonaModel);      
      this.model = (Persona) model;
   }
   
   /**
    * Get attached Persona model
    * @return model   A Persona Model
    */
   @Override
   public Persona getModel()
   {
      return (Persona) model;
   }
   
   /**
    * Find Persona Record by Id
    * Overrides Base Doa to convert to Persona model
    *
    * @return model   A Persona model found
    * @throws DaoException
    */
   @Override
   public Persona findById(Long id) throws DaoException
   {
      try
      {
         model = (Persona) super.findById(Persona.class, id);
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      
      
      return (Persona) model;
   }
   
   /**
    * Find a Persona by (unique) email address
    *
    * @param email   Email Address String
    * @return model   Persona Model
    * @throws DaoException
    */
   public Persona encontrarPorPersona(String per) throws DaoException
   {
	   int coma = per.indexOf(",");	   
	   String nombre = per.substring(coma+1).trim();	   
	   String apellido = per.substring(0, coma).trim();	   
	   
      try
      {
         model = (Persona) super.getEntityManager().
               createQuery("from Persona as p where p.apellido = ?1 and p.nombre = ?2")
                .setParameter(1, apellido)
                .setParameter(2, nombre)
                .getSingleResult();
         //super.setModel( PersonaModel );
         
      }
      catch(IllegalStateException e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      catch(IllegalArgumentException e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      catch(Exception e)
      {
         throw new DaoException("Find by Email Failed: " + e.getMessage());
      }
      
      
      return (Persona) model;
   }

public List<Persona> getTodasLasPersonas() throws DaoException {
	try
    {				         
       List <Persona> personas = 
      		 (List<Persona>) super.getEntityManager().
             createQuery("Select p from Persona as p")              
              .getResultList();
       //super.setModel( UsuarioModel );
       return personas;	
    }
    catch(IllegalStateException e)
    {
       throw new DaoException("Find by Email Failed: " + e.getMessage());
    }
    catch(IllegalArgumentException e)
    {
       throw new DaoException("Find by Email Failed: " + e.getMessage());
    }
    catch(Exception e)
    {
       throw new DaoException("Find by Email Failed: " + e.getMessage());
    }
}

}
